namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoBasicListForSearch : Page
    {
        protected DataGrid customerListDg;
        protected Image iBtnCustomer;
        protected UserPrincipal Principal = null;

        private void customerListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            string strCustomerId = e.CommandArgument.ToString();
            if (((str = cmd) != null) && (string.IsInterned(str) == "Edit"))
            {
                base.Response.Redirect("~/CIS/Customer/CustomerBasicInfoPageForSearch.aspx?CustomerId=" + strCustomerId, true);
            }
        }

        private void customerListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.customerListDg.CurrentPageIndex = e.NewPageIndex;
            this.DgDataBind();
        }

        private void DgDataBind()
        {
            if (base.Request.QueryString["IsSearch"] == "True")
            {
                string strCompanyName = this.Page.Session["CompanyName"].ToString();
                string strCustomerName = this.Page.Session["CustomerName"].ToString();
                string strCustomerTitle = this.Page.Session["CustomerTitleTbx"].ToString();
                int iCustomerLevel = Convert.ToInt32(this.Page.Session["CustomerLevel"]);
                try
                {
                    Area[] areaAry = this.Principal.OwnArea;
                    int areaLen = areaAry.Length;
                    DataTable dataTable = new DataTable();
                    int i = 0;
                    int y = 0;
                    for (i = 0; i < areaLen; i++)
                    {
                        DataTable dt = ManageCustomer.GetCustomerBasicInfo(areaAry[i].AreaId, strCompanyName, strCustomerName, strCustomerTitle, iCustomerLevel).Tables[0];
                        if (y == 0)
                        {
                            dataTable = dt.Clone();
                            y++;
                        }
                        foreach (DataRow tmpRow in dt.Rows)
                        {
                            dataTable.ImportRow(tmpRow);
                        }
                    }
                    this.customerListDg.DataSource = dataTable;
                    this.customerListDg.DataBind();
                }
                catch (Exception)
                {
                }
            }
        }

        private void FillPage()
        {
            this.DgDataBind();
        }

        private void InitializeComponent()
        {
            this.customerListDg.ItemCommand += new DataGridCommandEventHandler(this.customerListDg_ItemCommand);
            this.customerListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.customerListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFrom();
            this.customerListDg.PageSize = 14;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SelectAuth()
        {
            this.InitPage();
            this.FillPage();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            if (language == "cn")
            {
                this.iBtnCustomer.ImageUrl = "../images/title_customerinfo.jpg";
            }
            else if (language == "en")
            {
                this.iBtnCustomer.ImageUrl = "../images/title_customerinfoEn.jpg";
            }
            this.customerListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Name", language, path);
            this.customerListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Title", language, path);
            this.customerListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Company", language, path);
            this.customerListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Gender", language, path);
            this.customerListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Depart", language, path);
            this.customerListDg.Columns[5].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoListPage", "Edit", language, path);
        }
    }
}

